<?php

class Controller_Admin_User extends Controller_Admin {
	
	public function action_index() {
		$list = Lib\ZList::forge('users', array(
			'url' => 'admin/user/index',
			'page' => Input::get('p'),
			'limit' => 18
		));
		if(Input::get('s') != '')
		{
			$list->fulltext(array(
				array('name', Input::get('s'))
			));
			
			$list->set_vars(array('s' => Input::get('s')));
		}
		$list->order_by('name');
		$list = $list->get();
		
		$this->set('zlist', $list);
		$this->set('pagination', $list->pagination);


		$this->set('users', DB::select()->from('users')->order_by('created_at', 'desc')->execute()->as_array());
	}
	
	public function action_add() {
		
	}
	
	public function action_edit($id = false) {
		if($id === false) {
			Response::redirect('admin/user');
		}
		
		$user = Model_User::find_one_by_id($id);
		$this->set('user', $user);
	}

	public function action_delete() {
		if(Input::method() == 'POST') {
			$id = (array) Input::post('id');
			if(!$id) {
				Lib\Response::json(array(
					'alert' => 'Not exists',
					'redirect' => Uri::create('admin/user')
				), true);
			}

			DB::delete('users')->where('id', 'in', $id)->execute();
			
			Lib\Response::json(array(
				'notice' => 'Deleted successfully',
				'redirect' => Uri::create('admin/user')
			), true);
		}
	}

}